package com.etc.proxy.dynamic;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class WangBaProxyUtil implements InvocationHandler {

    ITarget target;

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        Object o = null;
        try {
            System.out.println("--befor: 1块--" + method.getName());
            o = method.invoke(target, args); // 所有的方法都会执行
            System.out.println("--after: 1块-----");
        } catch (Exception e) {
            System.out.println("--excpetion: 1块-----");
        } finally {
            System.out.println("--finally: 1块-----");
        }
        return o;
    }

    public ITarget newInstance() {
        return (ITarget) Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class[]{ITarget.class}, this);
    }
}
